//
//  Util.swift
//  learnSpriteKit
//
//  Created by Open on 3/19/15.
//  Copyright (c) 2015 open-groupe. All rights reserved.
//http://swifter.tips/regex/#operator
//正则表达式 30 分钟入门教程 http://deerchao.net/tutorials/regex/regex.htm
// 8 个常用正则表达 http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149

import Foundation

printf("hello")

struct RegexHelper {
    let regex: NSRegularExpression?
    
    init(_ pattern: String) {
        var error: NSError?
        regex = NSRegularExpression(pattern: pattern,
            options: .CaseInsensitive,
            error: &error)
    }
    
    func match(input: String) -> Bool {
        if  let matches = regex?.matchesInString(input, options: nil,
            range: NSMakeRange(0,cout(input))){
                return matches.count > 0
        }else {
            return false
        }
    }
    
   
    
}

infix operator =~ {
associativity none
precedence 130
}

func =~ (lhs: String, rhs: String) -> Bool {
    return RegexHelper(rhs).match(lhs)
}


func test()
{
    let mailPattern =
    "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
    let matcher = RegexHelper(mailPattern)
    let maybeMailAddress = "onev@onevcat.com"
    
    if matcher.match(maybeMailAddress) {
        println("有效的邮箱地址")
    }
}

